package lab4;

import lab2.Employee;
import lab2.Intern;
import lab2.RegularStaff;
import lab2.VisitorInterface;

import java.lang.reflect.InvocationTargetException;
import java.util.Random;

public class EmployeeActionHR implements VisitorInterface {
    int bestHours = Integer.MIN_VALUE;
    String bestName;
    int worstHours = Integer.MAX_VALUE;
    String worstName;

    static int PresetTime = 40;//每周工作40小时

    public EmployeeActionHR(){}
    public void visit(Employee employee){
        try {
            VisitorInterface.class.getDeclaredMethod("visit",employee.getClass()).invoke(this,employee);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void visit(RegularStaff regularStaff) {
        Random r = new Random();
        //随机生成员工的加班时长和请假时长
        int overtime = r.nextInt(30);
        regularStaff.setOvertime(overtime);
        int leaveTime = r.nextInt(40);
        regularStaff.setLeaveTime(leaveTime);
        //计算员工的总工时
        int workingHours = PresetTime + overtime + leaveTime;
        System.out.println("RegularStaff:"+regularStaff.getName()+"'s working time is"+workingHours);
        regularStaff.setWorkingHours(workingHours);
        if (workingHours > bestHours){
            bestHours = workingHours;
            bestName = regularStaff.getName();
        }
        if (workingHours < worstHours){
            worstHours = workingHours;
            worstName = regularStaff.getName();
        }
    }

    @Override
    public void visit(Intern intern) {
        Random r = new Random();
        int hours = r.nextInt(50);
        System.out.println("Intern:"+intern.getName()+"'s working time is"+hours);
        intern.setWorkingHours(hours);
        if (hours > bestHours){
            bestHours = hours;
            bestName = intern.getName();
        }
        if (hours < worstHours){
            worstHours = hours;
            worstName = intern.getName();
        }
    }

    public void visit(PartTimeJob partTimeJob){
        Random r = new Random();
        int hours = r.nextInt(50);
        System.out.println("PartTimeJob:"+partTimeJob.getName()+"'s working time is"+hours);
        partTimeJob.setWorkingHours(hours);
    }

    public String getBestEmployee(){
        System.out.println("The best employee is"+bestName);
        return bestName;
    }

    public String getWorstName(){
        System.out.println("The worst employee is"+worstName);
        return worstName;
    }
}
